﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HXDNB.DAL;
using HXDNB.CMS.App_Code;
using HXDNB.Infrastructure;

namespace HXDNB.CMS.Controllers
{
    public class SaleOrderController : BaseController
    {
        SaleOrderBLL bll = new SaleOrderBLL();

        public ActionResult List(DateTime? OrderDate, int Customer = -1, int ProductID = -1, int State = -1)
        {
            var list = bll.GetList(OrderDate, Customer, ProductID, State);
            var pag = new Paging<SaleOrderList>(list);
            ViewBag.PagList = pag;
            return View();
        }

        public ActionResult Info(int? id)
        {
            var info = new SaleOrder();
            if (id != null)
            {
                info = bll.GetByKey((int)id);
            }
            return View(info);
        }

        [HttpPost, ValidateInput(false)]
        public ActionResult Info(SaleOrder info)
        {
            bool result = false;
            if (info.ID > 0)
            {              
                result = bll.update(info);               
            }
            else
            {
                info.CreateTime = DateTime.Now;
                info.PayState = (int)SalePayState.待支付;
                info.State = (int)SaleOrderState.待发货;
                info.ID = bll.insert(info);
                result = info.ID > 0;
            }
            return RedirectToAction("Info", new { id = info.ID, result = result ? "Y" : "N" });
        }

        [HttpPost]
        public string Delete(string id = "")
        {
            bool result = false;
            foreach (string item in id.Split(',').Where(item => !string.IsNullOrWhiteSpace(item)))
            {
                SaleOrder Info = bll.GetByKey(int.Parse(item));
                if (((SaleOrderState)Info.State) == SaleOrderState.待发货)
                {
                    result = bll.Delete(int.Parse(item));
                    if (!result)
                    {
                        break;
                    }
                }
            }
            return result.ToString();
        }
        [HttpPost]
        public string SetPayState(int id)
        {
            bool result = false;
            if (id > 0)
            {
                SaleOrder Info = bll.GetByKey(id);
                Info.PayState = (int)SalePayState.已支付;
                Info.ReceivedMoneyTime = DateTime.Now;
                result = bll.update(Info);
            }
            return result.ToString();
        }
        [HttpPost]
        public string SetState(int id)
        {
            bool result = false;
            if (id > 0)
            {
                SaleOrder Info = bll.GetByKey(id);
                Info.State = (int)SaleOrderState.已发货;
                Info.SendTime = DateTime.Now;
                ProductBLL ProdBll = new ProductBLL();
                ProdBll.AddStock((int)Info.ProductID, -(int)Info.Num);
                result = bll.update(Info);
            }
            return result.ToString();
        }
        [HttpPost]
        public string Back(int id)
        {
            bool result = false;
            if (id > 0)
            {
                SaleOrder Info = bll.GetByKey(id);
                Info.State = (int)SaleOrderState.已退货;
                ProductBLL ProdBll = new ProductBLL();
                ProdBll.AddStock((int)Info.ProductID, (int)Info.Num);
                result = bll.update(Info);
            }
            return result.ToString();
        }
    }
}
